
// DmViewerView.cpp : implementation of the CDmViewerView class
//

#include "stdafx.h"
#include "DmViewer.h"
#include "OSGViewMaker.h"
#include "DmViewerDoc.h"
#include "DmViewerView.h"
#include <osg\ref_ptr>
#include<osg\Group>

#include <osg\ShapeDrawable>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDmViewerView

IMPLEMENT_DYNCREATE(CDmViewerView, CView)

BEGIN_MESSAGE_MAP(CDmViewerView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CDmViewerView::OnFilePrintPreview)
	ON_WM_DESTROY()
END_MESSAGE_MAP()

// CDmViewerView construction/destruction

CDmViewerView::CDmViewerView()
{
	// TODO: add construction code here	
	m_pOsgViewer = new COSGViewer();

}

CDmViewerView::~CDmViewerView()
{
	if(NULL != m_pOsgViewer)
	{
		delete m_pOsgViewer;
	}
}

BOOL CDmViewerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CDmViewerView drawing

void CDmViewerView::OnDraw(CDC* /*pDC*/)
{
	CDmViewerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
}


// CDmViewerView printing


void CDmViewerView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL CDmViewerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDmViewerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDmViewerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CDmViewerView::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CDmViewerView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// CDmViewerView diagnostics

#ifdef _DEBUG
void CDmViewerView::AssertValid() const
{
	CView::AssertValid();
}

void CDmViewerView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDmViewerDoc* CDmViewerView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDmViewerDoc)));
	return (CDmViewerDoc*)m_pDocument;
}
#endif //_DEBUG


// CDmViewerView message handlers

BOOL CDmViewerView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
	// TODO: Add your specialized code here and/or call the base class
	return CView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
	
	//init group
	 
}

void CDmViewerView::OnInitialUpdate()
{
	CView::OnInitialUpdate();

	// TODO: Add your specialized code here and/or call the base class
	m_pOsgViewer->Attach(this->GetSafeHwnd());
}

void CDmViewerView::OnDestroy()
{
	CView::OnDestroy();

	m_pOsgViewer->Detach();
}

void CDmViewerView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	// TODO: Add your specialized code here and/or call the base class
	CDmViewerDoc* pDoc = GetDocument();
	if(NULL != pDoc)
	{
		//init group

		DicomImage* pImage = new DicomImage(pDoc->GetPathName());
		if((NULL != pImage) && (EIS_Normal == pImage->getStatus()))
		{
			osg::ref_ptr<osg::Image> osgImage (new osg::Image());
			ULONG bytesAllocated = 0;
			LPVOID voidData = NULL;
			bytesAllocated  = pImage->createWindowsDIB(voidData, 0, 0, 8, 1);

			osgImage->setImage(pImage->getWidth(), pImage->getHeight(), 8, GL_RGB, GL_LUMINANCE, GL_UNSIGNED_BYTE, (unsigned char*) voidData, osg::Image::USE_NEW_DELETE);
			osg::ref_ptr<osg::Geode> image(osg::createGeodeForImage(osgImage.get()));
			osg::ref_ptr<osg::Group> imgRoot(new osg::Group()); 
			imgRoot->addChild(image.get());
			m_pOsgViewer->SetSceneData(imgRoot);
		}
		delete pImage;
	}
}
